/**
 * 仓库
 *
 * @author sxf
 * @email sxf02615@163.com
 * @date 2025/1/15
 */
package com.sxf.crm.repository;


import com.sxf.crm.entity.CustomerDepartment;
import com.sxf.crm.entity.CustomerDepartmentId;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface CustomerDepartmentRepository extends JpaRepository<CustomerDepartment, CustomerDepartmentId> {
    @Query("SELECT cd FROM CustomerDepartment cd " +
           "LEFT JOIN FETCH cd.customer " +
           "LEFT JOIN FETCH cd.department " +
           "LEFT JOIN FETCH cd.sales " +
           "WHERE cd.customer.id = :customerId")
    List<CustomerDepartment> findByCustomerId(@Param("customerId") Long customerId);

    @Modifying
    @Query("DELETE FROM CustomerDepartment cd WHERE cd.customer.id = :customerId AND cd.department.id = :departmentId")
    void deleteByCustomerIdAndDepartmentId(@Param("customerId") Long customerId, @Param("departmentId") Long departmentId);
} 